package com.dms.modules.message.controller;

import com.dms.common.api.ApiResponse;
import com.dms.modules.message.dto.MessageDTO;
import com.dms.modules.message.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@Validated
@RestController
@RequestMapping("/api/admin/messages")
@Api(tags = "消息管理后台")
@PreAuthorize("hasRole('ADMIN')")
public class MessageAdminController {

    @Autowired
    private MessageService messageService;

    @PostMapping
    @ApiOperation("发送消息")
    public ApiResponse<Long> sendMessage(@Valid @RequestBody MessageDTO messageDTO) {
        try {
            Long messageId = messageService.sendMessage(messageDTO);
            return ApiResponse.success(messageId);
        } catch (Exception e) {
            log.error("发送消息失败: {}", e.getMessage(), e);
            return ApiResponse.error(e);
        }
    }

    @PutMapping("/{id}/recall")
    @ApiOperation("撤回消息")
    public ApiResponse<Boolean> recallMessage(@PathVariable Long id) {
        try {
            boolean result = messageService.recallMessage(id);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("撤回消息失败: id={}, error={}", id, e.getMessage(), e);
            return ApiResponse.error(e);
        }
    }
} 